package com.ruoyi.manage.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.domain.CrRecruitment;
import com.ruoyi.manage.service.ICrRecruitmentService;

/**
 * 社团招聘Controller
 * 
 * @author ruoyi
 * @date 2024-09-24
 */
@RestController
@RequestMapping("/campus/recruitment")
public class CrRecruitmentController extends BaseController
{
    @Autowired
    private ICrRecruitmentService crRecruitmentService;

    /**
     * 查询社团招聘列表
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:list') or @ss.hasRole('student')")
    @GetMapping("/list")
    public TableDataInfo list(CrRecruitment crRecruitment)
    {
        startPage();
        List<CrRecruitment> list = crRecruitmentService.selectCrRecruitmentList(crRecruitment);
        return getDataTable(list);
    }

    /**
     * 导出社团招聘列表
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:export')")
    @Log(title = "社团招聘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrRecruitment crRecruitment)
    {
        List<CrRecruitment> list = crRecruitmentService.exportRecruitments(crRecruitment);
        ExcelUtil<CrRecruitment> util = new ExcelUtil<CrRecruitment>(CrRecruitment.class);
        util.exportExcel(response, list, "社团招聘数据");
    }

    /**
     * 获取社团招聘详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:query') or @ss.hasRole('student') or @ss.hasRole('club')")
    @GetMapping(value = "/{recruitmentId}")
    public AjaxResult getInfo(@PathVariable("recruitmentId") Long recruitmentId)
    {
        return success(crRecruitmentService.selectCrRecruitmentByRecruitmentId(recruitmentId));
    }

    /**
     * 新增社团招聘
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:add')")
    @Log(title = "社团招聘", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrRecruitment crRecruitment)
    {
        return toAjax(crRecruitmentService.insertCrRecruitment(crRecruitment));
    }

    /**
     * 修改社团招聘
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:edit') or @ss.hasRole('club')")
    @Log(title = "社团招聘", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrRecruitment crRecruitment)
    {
        return toAjax(crRecruitmentService.updateCrRecruitment(crRecruitment));
    }

    /**
     * 删除社团招聘
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:remove') or @ss.hasRole('club')")
    @Log(title = "社团招聘", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recruitmentIds}")
    public AjaxResult remove(@PathVariable Long[] recruitmentIds)
    {
        return toAjax(crRecruitmentService.deleteCrRecruitmentByRecruitmentIds(recruitmentIds));
    }

    // ========== 业务相关接口 ==========

    /**
     * 发布招聘信息
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:publish') or @ss.hasRole('club')")
    @Log(title = "发布招聘", businessType = BusinessType.INSERT)
    @PostMapping("/publish")
    public AjaxResult publishRecruitment(@RequestBody CrRecruitment crRecruitment)
    {
        return toAjax(crRecruitmentService.publishRecruitment(crRecruitment));
    }

    /**
     * 关闭招聘
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:edit') or @ss.hasRole('club')")
    @Log(title = "关闭招聘", businessType = BusinessType.UPDATE)
    @PutMapping("/close/{recruitmentId}")
    public AjaxResult closeRecruitment(@PathVariable Long recruitmentId)
    {
        Long clubId = getUserId(); // 可根据实际情况调整获取方式
        return toAjax(crRecruitmentService.closeRecruitment(recruitmentId, clubId));
    }

    /**
     * 重新开放招聘
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:edit') or @ss.hasRole('club')")
    @Log(title = "重新开放招聘", businessType = BusinessType.UPDATE)
    @PutMapping("/reopen/{recruitmentId}")
    public AjaxResult reopenRecruitment(@PathVariable Long recruitmentId)
    {
        Long clubId = getUserId(); // 可根据实际情况调整获取方式
        return toAjax(crRecruitmentService.reopenRecruitment(recruitmentId, clubId));
    }

    /**
     * 增加浏览量
     */
    @PostMapping("/increaseView/{recruitmentId}")
    public AjaxResult increaseViewCount(@PathVariable Long recruitmentId)
    {
        return toAjax(crRecruitmentService.increaseViewCount(recruitmentId));
    }

    /**
     * 根据社团ID查询招聘列表
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:list') or @ss.hasRole('student') or @ss.hasRole('club')")
    @GetMapping("/club/{clubId}")
    public AjaxResult getRecruitmentsByClub(@PathVariable Long clubId)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.selectRecruitmentsByClubId(clubId);
        return success(recruitments);
    }

    /**
     * 根据状态查询招聘列表
     */
    @GetMapping("/status/{status}")
    public AjaxResult getRecruitmentsByStatus(@PathVariable String status)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.selectRecruitmentsByStatus(status);
        return success(recruitments);
    }

    /**
     * 搜索招聘信息
     */
    @GetMapping("/search")
    public AjaxResult searchRecruitments(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String clubType,
            @RequestParam(required = false) String status)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.searchRecruitments(keyword, clubType, status);
        return success(recruitments);
    }

    /**
     * 获取热门招聘
     */
    @GetMapping("/popular")
    public AjaxResult getPopularRecruitments(@RequestParam(defaultValue = "10") int limit)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.getPopularRecruitments(limit);
        return success(recruitments);
    }

    /**
     * 获取最新招聘
     */
    @GetMapping("/latest")
    public AjaxResult getLatestRecruitments(@RequestParam(defaultValue = "10") int limit)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.getLatestRecruitments(limit);
        return success(recruitments);
    }

    /**
     * 获取即将截止的招聘
     */
    @GetMapping("/expiring")
    public AjaxResult getExpiringRecruitments(@RequestParam(defaultValue = "7") int days)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.getExpiringRecruitments(days);
        return success(recruitments);
    }

    /**
     * 获取社团招聘统计
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:list')")
    @GetMapping("/statistics/club/{clubId}")
    public AjaxResult getClubRecruitmentStatistics(@PathVariable Long clubId)
    {
        Map<String, Object> statistics = crRecruitmentService.getClubRecruitmentStatistics(clubId);
        return success(statistics);
    }

    /**
     * 获取招聘统计信息
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:list')")
    @GetMapping("/statistics/{recruitmentId}")
    public AjaxResult getRecruitmentStatistics(@PathVariable Long recruitmentId)
    {
        Map<String, Object> statistics = crRecruitmentService.getRecruitmentStatistics(recruitmentId);
        return success(statistics);
    }

    /**
     * 检查招聘是否可申请
     */
    @GetMapping("/checkAvailable/{recruitmentId}")
    public AjaxResult isRecruitmentAvailable(@PathVariable Long recruitmentId)
    {
        boolean isAvailable = crRecruitmentService.isRecruitmentAvailable(recruitmentId);
        return success(isAvailable);
    }

    /**
     * 获取推荐招聘（基于学生兴趣和技能）
     */
    @GetMapping("/recommended/{studentId}")
    public AjaxResult getRecommendedRecruitments(@PathVariable Long studentId, 
                                               @RequestParam(defaultValue = "5") int limit)
    {
        List<CrRecruitment> recruitments = crRecruitmentService.getRecommendedRecruitments(studentId, limit);
        return success(recruitments);
    }

    /**
     * 更新招聘申请统计
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:edit')")
    @PutMapping("/updateStats/{recruitmentId}")
    public AjaxResult updateRecruitmentApplicationStats(@PathVariable Long recruitmentId)
    {
        return toAjax(crRecruitmentService.updateRecruitmentApplicationStats(recruitmentId));
    }

    /**
     * 批量更新招聘状态
     */
    @PreAuthorize("@ss.hasPermi('manage:recruitment:edit')")
    @Log(title = "批量更新招聘状态", businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdateStatus")
    public AjaxResult batchUpdateRecruitmentStatus(@RequestBody Map<String, Object> params)
    {
        Long[] recruitmentIds = ((List<Long>) params.get("recruitmentIds")).toArray(new Long[0]);
        String status = params.get("status").toString();
        
        int result = crRecruitmentService.batchUpdateRecruitmentStatus(recruitmentIds, status);
        return toAjax(result);
    }
}